<template>

  <div id="app">
    <!-- 顶部导航栏 -->
    <el-header
      class="header"
      v-if="!shouldHideHeader"
    >
     <div class="logo-container">
       <img :src="logoUrl" alt="中国人寿" class="logo-img" />
     </div>
      <div class="header-right">
        <template v-if="!isLoginPage">
          <template v-if="currentUser">
            <el-dropdown>
              <span class="user-info">
                <el-avatar :size="36" :src="currentUser.avatar || defaultAvatar" />
                <div class="user-details">
                  <div class="username">{{ currentUser.phone }}</div>
                  <div class="user-status">在线</div>
                </div>
                <el-icon class="dropdown-icon"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
<!--                导航栏条件渲染-->
                <el-dropdown-menu>
                   <el-dropdown-item v-if="role === 'admin'" @click="router.push('/admincenter')">管理中心</el-dropdown-item>
                   <el-dropdown-item v-if="role === 'user'" @click="goToUserCenter">用户中心</el-dropdown-item>
                   <el-dropdown-item @click="logout">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <el-button v-else type="text" @click="goToLogin" class="login-btn">
            登录
          </el-button>
        </template>
      </div>
    </el-header>

    <!-- 网站介绍横幅 - 添加对齐容器 -->
    <div
      class="banner"
      v-if="!shouldHideHeader && !isLoginPage"
    >
      <div class="banner-content">
        <h1>{{ weatherData.tell }}</h1>
        <p>{{ weatherData.weather }}</p>
      </div>
    </div>

    <!-- 顶部主导航栏 -->
    <div class="top-nav-container" v-if="!shouldHideHeader && !isLoginPage">
      <el-menu
        class="main-nav"
        mode="horizontal"
        :default-active="activeIndex"
        @select="handleSelect"
        background-color="#f5f7fa"
        text-color="#333"
        active-text-color="#409EFF"
      >
        <el-menu-item index="1">首页</el-menu-item>
        <el-menu-item index="2">产品中心</el-menu-item>
        <el-menu-item index="3">解决方案</el-menu-item>
        <el-menu-item index="4">客户案例</el-menu-item>
        <el-menu-item index="5">关于我们</el-menu-item>
        <el-menu-item index="6">查看附近的公司</el-menu-item>
        <el-menu-item index="5"></el-menu-item>
      </el-menu>
    </div>

    <!-- 主内容区 -->
    <el-main :class="{ 'full-height-main': shouldHideHeader }">
      <router-view @login-success="handleLoginSuccess"/>
    </el-main>

    <!-- 底部导航栏 -->
    <div class="bottom-nav-container" v-if="!shouldHideHeader && !isLoginPage">
      <el-menu
        class="bottom-nav"
        mode="horizontal"
        :default-active="activeIndex"
        @select="handleSelect"
        background-color="#2e384d"
        text-color="#d1d3e2"
        active-text-color="#ffffff"
      >
        <el-menu-item index="7">医疗保险专业咨询</el-menu-item>
        <el-menu-item index="8">事故保险专业咨询</el-menu-item>
        <el-menu-item index="9">财产保险专业咨询</el-menu-item>

        <el-menu-item index="6"></el-menu-item>
      </el-menu>
    </div>

    <!-- 页脚 -->
    <el-footer
      class="footer"
      v-if="!shouldHideHeader && !isLoginPage"
    >
      <div class="footer-content">
        <div class="footer-section">
          <h4>关于我们</h4>
          <p>专业的保险金融服务提供商</p>
        </div>
        <div class="footer-section">
          <h4>联系我们</h4>
          <p>电话: 400-123-4567</p>
          <p>邮箱: 793107248@qq.com</p>
        </div>
        <div class="footer-section">
          <h4>关注我们</h4>
          <div class="social-icons">
            <el-icon :size="20"><share /></el-icon>
            <el-icon :size="20"><chat-dot-round /></el-icon>
            <el-icon :size="20"><platform /></el-icon>
          </div>
        </div>
      </div>
      <div class="copyright">
        © 2023 MyVue3 Project. All rights reserved.
      </div>
    </el-footer>
  </div>
</template>

<script setup>
   // <script setup> 顶部加
import logoUrl from '@/assets/logo.jpg'
import { computed, ref, onMounted, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import axios from "axios";
import { ArrowDown, Share, ChatDotRound, Platform } from '@element-plus/icons-vue'

// 导航栏角色判断要用响应式变量
const role = ref(localStorage.getItem('role'))

// 登录/退出后记得同步 role.value
const updateRole = () => {
  role.value = localStorage.getItem('role')
}
onMounted(() => {
  updateRole()
})
const router = useRouter()
const route = useRoute()

// 默认头像
const defaultAvatar = ref('https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png')

// 当前登录用户
const currentUser = ref(null)

// 检查登录状态
const isLoginPage = computed(() => ['/login', '/houlogin'].includes(route.path))

// 检查是否需要隐藏页头
const shouldHideHeader = computed(() => {
  return route.path.startsWith('/usercenter') ||
         route.path.startsWith('/products/') ||
         ['/login', '/houlogin'].includes(route.path)
})
// 从 localStorage 获取用户信息
const updateUserInfo = () => {
  const phone = localStorage.getItem('phone')
  if (phone) {
    currentUser.value = {
      phone,
      name: phone,
      avatar: defaultAvatar.value
    }
    return true
  }
  return false
}

// 跳转到用户中心
const goToUserCenter = () => {
  router.push('/usercenter')
}

// 处理登录成功事件
const handleLoginSuccess = (userInfo) => {
  localStorage.setItem('phone', userInfo.phone)
  currentUser.value = {
    phone: userInfo.phone,
    name: userInfo.phone,
    avatar: defaultAvatar.value
  }
}

let weatherData = ref({ tell: '欢迎使用保险金融服务平台', weather: '晴，25°C' });

const Weather = () => {
  axios.get('http://127.0.0.1:8000/ai/weather/', {params: {user_id: localStorage.getItem('user_id')}})
  .then(response => {
    console.log(response.data);
    weatherData.value = response.data;
  })
  .catch(() => {
    weatherData.value = { tell: '欢迎使用保险金融服务平台', weather: '晴，25°C' };
  })
}

// 初始化
onMounted(() => {
  updateUserInfo()
  watch(
    () => route.path,
    () => {
      updateUserInfo()
    },
    { immediate: true }
  )
  Weather();
})

// 导航激活项
const activeIndex = ref('1')

const goToLogin = () => {
  router.push('/login')
}

const handleSelect = (index) => {
  const routes = {
    '1': '/',
    '2': '/products',
    '3': '/solution',
    '4': '/list',
    '5': '/about',
    '6': '/map',
    '7': '/ai_m',
    '8': '/ai_a',
    '9': '/ai_p',
  }
  if (routes[index]) {
    router.push(routes[index])
  }
}

// 退出登录
const logout = () => {
  localStorage.removeItem('role')
  localStorage.removeItem('token')
  localStorage.removeItem('phone')
  localStorage.removeItem('user_id')
  localStorage.removeItem('retoken')
  updateRole() // 推荐用这个方法刷新role
  currentUser.value = null
  router.push('/login')
}

</script>

<style scoped>
#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  font-family: 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', Arial, sans-serif;
  background-color: #f8f9fc;
}

/* ========== 顶部导航栏 ========== */
.header {
  background: linear-gradient(to right, #ffffff, #1e90ff);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 30px;
  height: 70px;
  box-shadow: 0 2px 6px rgba(0, 255, 255, 0.9);
  position: relative;
  z-index: 1000;
}

.logo-container {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.logo {
  font-size: 24px;
  font-weight: 700;
  color: white;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.logo-subtitle {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.8);
  letter-spacing: 2px;
  margin-top: 2px;
  text-transform: uppercase;
}

.user-info {
  display: flex;
  align-items: center;
  padding: 6px 15px 6px 10px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: 30px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: translateY(-2px);
}

.user-details {
  margin-left: 12px;
  text-align: left;
}

.username {
  color: white;
  font-weight: 500;
  font-size: 14px;
}

.user-status {
  color: rgba(255, 255, 255, 0.8);
  font-size: 12px;
  margin-top: 2px;
}

.dropdown-icon {
  color: white;
  margin-left: 8px;
  font-size: 14px;
}

.login-btn {
  color: white !important;
  font-size: 16px;
  padding: 10px 15px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 20px;
  transition: all 0.3s;
}

.login-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* ========== 横幅区域 - 对齐修复 ========== */
.banner {
  padding: 40px 20px;
  text-align: center;
  background: linear-gradient(135deg, #f0f4ff 0%, #e1e7ff 100%);
  position: relative;
  overflow: hidden;
}

.banner::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #2e59d9, #4e73df, #2e59d9);
}

.banner-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.banner h1 {
  font-size: 2.2rem;
  margin-bottom: 12px;
  color: #2e384d;
  font-weight: 700;
  line-height: 1.3;
}

.banner p {
  font-size: 1.1rem;
  color: #6e707e;
  line-height: 1.6;
  max-width: 800px;
  margin: 0 auto;
}

/* ========== 顶部主导航栏容器 ========== */
.top-nav-container {
  background: white;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
  position: relative;
  z-index: 100;
  display: flex;
  justify-content: center;

}

/* ========== 主导航栏 ========== */
.main-nav {
  background: white !important;
  border: none !important;
  max-width: 1200px;
  width: 100%;
}

.el-menu-item {
  font-weight: 500;
  transition: all 0.3s ease;
  height: 60px;
  line-height: 60px;
  padding: 0 25px;
  flex: 1;
  min-width: 0;
  text-align: center;
  display: flex;
  justify-content: center;
}

.el-menu-item:hover {
  background-color: rgba(78, 115, 223, 0.05) !important;
}

.el-menu-item.is-active {
  border-bottom: 3px solid #4e73df;
  color: #2e59d9 !important;
  font-weight: 600;
}

/* ========== 底部导航栏容器 ========== */
.bottom-nav-container {
  background: #2e384d;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  position: relative;
  z-index: 100;
  display: flex;
  justify-content: center;
  padding: 10px 0;
}

/* ========== 底部导航栏 ========== */
.bottom-nav {
  max-width: 1200px;
  width: 100%;
  border: none !important;
}

.bottom-nav .el-menu-item {
  color: #d1d3e2 !important;
  font-size: 14px;
  height: 50px;
  line-height: 50px;
  transition: all 0.3s ease;
}

.bottom-nav .el-menu-item:hover {
  background-color: rgba(255, 255, 255, 0.1) !important;
  color: white !important;
}

.bottom-nav .el-menu-item.is-active {
  color: white !important;
  font-weight: 600;
  border-bottom: 3px solid #4e73df;
}

/* ========== 主内容区 ========== */
.el-main {
  flex: 1;
  padding: 30px;
  background-color: #f8f9fc;
}

.full-height-main {
  height: calc(100vh - 20px);
  padding: 10px;
}

/* ========== 页脚 ========== */
.footer {
  background: #1a202c;
  color: white;
  padding: 40px 20px 20px;
  position: relative;
}

.footer::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 4px;
  background: linear-gradient(90deg, #2e59d9, #4e73df, #2e59d9);
}

.footer-content {
  display: flex;
  justify-content: space-around;
  max-width: 1200px;
  margin: 0 auto 30px;
  flex-wrap: wrap;
}

.footer-section {
  flex: 1;
  min-width: 250px;
  margin-bottom: 20px;
  padding: 0 15px;
}

.footer-section h4 {
  font-size: 18px;
  margin-bottom: 15px;
  position: relative;
  padding-bottom: 10px;
  color: white;
}

.footer-section h4::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 50px;
  height: 2px;
  background: #4e73df;
}

.footer-section p {
  margin: 8px 0;
  color: #d1d3e2;
  font-size: 14px;
}

.social-icons {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.social-icons .el-icon {
  background: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s;
  color: white;
}

.social-icons .el-icon:hover {
  background: #4e73df;
  transform: translateY(-3px);
}

.copyright {
  text-align: center;
  padding-top: 20px;
  border-top: 1px solid rgba(255, 255, 255, 0.1);
  color: #a0a4b8;
  font-size: 14px;
}

/* ========== 响应式调整 ========== */
@media (max-width: 992px) {
  .el-menu-item {
    padding: 0 15px;
    font-size: 14px;
  }

  .bottom-nav .el-menu-item {
    font-size: 13px;
    padding: 0 12px;
  }
}

@media (max-width: 768px) {
  .header {
    padding: 0 15px;
    height: 60px;
  }

  .logo {
    font-size: 20px;
  }

  .logo-subtitle {
    font-size: 10px;
  }

  .user-info {
    padding: 5px 10px 5px 8px;
  }

  .username {
    font-size: 12px;
  }

  .banner {
    padding: 25px 15px;
  }

  .banner h1 {
    font-size: 1.8rem;
  }

  .banner p {
    font-size: 1rem;
  }

  .el-menu-item {
    padding: 0 10px;
    font-size: 13px;
  }

  .bottom-nav .el-menu-item {
    font-size: 12px;
    padding: 0 8px;
  }

  .footer-content {
    flex-direction: column;
    align-items: center;
    text-align: center;
  }

  .footer-section h4::after {
    left: 50%;
    transform: translateX(-50%);
  }
}


.logo-img {
  height: 150px;
  margin-bottom: 5px;
  margin-right: 10px;
  vertical-align: middle;
}
.logo-subtitle {
  font-size: 14px;
  color: rgba(255,255,255,0.9);
  margin-top: 2px;
  font-weight: 500;
}


@media (max-width: 576px) {
  .el-menu-item {
    padding: 0 8px;
    font-size: 12px;
    height: 50px;
    line-height: 50px;
  }

  .bottom-nav .el-menu-item {
    font-size: 11px;
    padding: 0 5px;
    height: 40px;
    line-height: 40px;
  }

  .banner h1 {
    font-size: 1.5rem;
  }

  .bottom-nav-container {
    padding: 5px 0;
  }
}
</style>